#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QPainter>
#include <QImage>
#include <QPicture>

Widget::Widget(QWidget *parent)
        : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    /////////// Pixmap绘图设备，专门为平台做了显示的优化 /////////////
    QPixmap pix(300, 300);
    pix.fill(Qt::white);    // 设置 背景色，默认是黑色

    // 声明画家
    QPainter painter(&pix);
    painter.setPen(QPen(Qt::green));    // 设置画笔颜色
    painter.drawEllipse(QPoint(150, 150), 100, 100);
    pix.save("D:\\pix.png");

    ////////////// QImage绘图设备，可以对像素进行访问 ////////////////////
    QImage img(300, 300, QImage::Format_RGB32);
    img.fill(Qt::white);

    QPainter painter2(&img);
    painter2.setPen(QPen(Qt::blue));
    painter2.drawEllipse(QPoint(150, 150), 100, 100);
    img.save("D:\\image.png");

    ////////////// QPicture绘图设备  可以记录和重现绘图指令 ////////////////////
    QPicture pic;
    QPainter painter3;
    painter3.begin(&pic);   // 开始往pic上画
    painter3.setPen(QPen(Qt::cyan));
    painter3.drawEllipse(QPoint(150, 150), 100, 100);
    painter3.end();          // 结束画画
    pic.save("D:\\pic.zt");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    // 利用QImage 对像素进行修改
    QImage img;
    img.load(":/Image/timg.gif");

    // 修改像素点
    for (int i = 50; i < 100; ++i) {
        for (int j = 50; j < 100; ++j) {
//            QRgb value = qRgb(255, 0, 0);
            img.setPixel(i, j, qRgb(255, 0, 0));
        }
    }

    painter.drawImage(0, 0, img);

    // 重现 pic
    QPicture pic;
    pic.load("D:\\pic.zt");
    painter.drawPicture(200,0, pic);
}
